import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get/instance_manager.dart';

class CountPage extends StatelessWidget {
  CountPage({super.key});
  final CountController controller =
      Get.put<CountController>(CountController());
  final CountObsController obsController = Get.put(CountObsController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('count'),
      ),
      body: Column(
        children: const [
          GetBuilderText(),
          ObsText(),
        ],
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            child: const Icon(Icons.add),
            onPressed: () => controller.increase(),
          ),
          FloatingActionButton(
            child: const Icon(Icons.access_alarm_outlined),
            onPressed: () => obsController.increase(),
          )
        ],
      ),
    );
  }
}

class GetBuilderText extends StatelessWidget {
  const GetBuilderText({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CountController>(
      init: CountController(),
      initState: (_) {},
      builder: (CountController controller) {
        return Text(
          '${controller.count}',
          style: const TextStyle(fontSize: 30),
        );
      },
    );
  }
}

class CountController extends GetxController {
  int _count = 0;

  void increase() {
    _count += 1;
    update();
  }

  int get count {
    return _count;
  }
}

class CountObsController extends GetxController {
  final count = 0.obs;
  void increase() => count.value += 1;
}

class ObsText extends StatelessWidget {
  const ObsText({super.key});

  @override
  Widget build(BuildContext context) {
    return GetX<CountObsController>(
      builder: (CountObsController controller) {
        return Text(
          '${controller.count.value}',
          style: const TextStyle(fontSize: 50),
        );
      },
    );
  }
}
